﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LoadingB.Config;
using System.IO;
using Microsoft.Win32;

namespace LoadingB.Windows
{
    public partial class Form_Setup : Form
    {
        public Form_Setup()
        {
            InitializeComponent();
        }

        private void Form_Setup_Load(object sender, EventArgs e)
        {
            // 定义 显示托盘 选项及其代表数值
            var comboBox2Items = new Dictionary<string, int>
            {
                { "否", 0 },
                { "是", 1 }
            };

            // 添加选项到 comboBox2
            foreach (var item in comboBox2Items)
            {
                comboBox2.Items.Add(item.Key);
            }

            // 定义 自动启动 选项及其代表数值
            var comboBox3Items = new Dictionary<string, int>
            {
                { "否", 0 },
                { "是", 1 }
            };

            // 添加选项到 comboBox3
            foreach (var item in comboBox3Items)
            {
                comboBox3.Items.Add(item.Key);
            }

            // 获取并显示所有可用的显示器序号
            foreach (var screen in Screen.AllScreens)
            {
                comboBox1.Items.Add(Array.IndexOf(Screen.AllScreens, screen));
            }

            // 检查配置文件是否存在
            string configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CoreFile.config");
            if (File.Exists(configFilePath))
            {
                // 加载配置信息
                LoadConfig();
            }
            else
            {
                // 显示默认信息
                SetDefaultConfig();
            }
        }

        // 加载配置
        private void LoadConfig()
        {
            // 加载显示器序号
            comboBox1.SelectedIndex = Variable.Screen;

            // 加载网址
            textBox1.Text = Variable.Url;

            // 加载主机名称
            textBox2.Text = Variable.Host;

            // 加载托盘选项
            comboBox2.SelectedIndex = Variable.Tray;

            // 加载自动启动选项
            comboBox3.SelectedIndex = Variable.Start;
        }

        // 设置默认配置
        private void SetDefaultConfig()
        {
            // 设置默认显示器序号
            comboBox1.SelectedIndex = 1;

            // 设置默认网址
            textBox1.Text = "http://127.0.0.1:8080";

            // 设置默认主机名称
            textBox2.Text = "localhost";

            // 设置默认托盘选项为“是”
            comboBox2.SelectedIndex = comboBox2.Items.IndexOf("是");

            // 设置默认自动启动选项为“否”
            comboBox3.SelectedIndex = comboBox3.Items.IndexOf("否");
        }

        // 保存配置
        private void button1_Click(object sender, EventArgs e)
        {
            // 保存显示器序号
            Variable.Screen = comboBox1.SelectedIndex;
            Variable.Url = textBox1.Text;
            Variable.Host = textBox2.Text;
            Variable.Tray = comboBox2.SelectedIndex;
            Variable.Start = comboBox3.SelectedIndex;

            // 保存到配置文件
            ConfigManager configManager = new ConfigManager();
            configManager.Write("Screen", Variable.Screen.ToString());
            configManager.Write("Url", Variable.Url);
            configManager.Write("Host", Variable.Host);
            configManager.Write("Tray", Variable.Tray.ToString());
            configManager.Write("Start", Variable.Start.ToString());

            // 设置软件是否跟随系统启动
            SetStartup(Variable.Start == 1);

            // 显示保存成功的消息
            MessageBox.Show("配置已保存，请重启软件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            // 退出应用程序
            Application.Exit();
        }

        // 设置软件是否跟随系统启动
        private void SetStartup(bool enable)
        {
            string runKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
            string appName = "LoadingB";
            string appPath = Application.ExecutablePath;

            using (RegistryKey key = Registry.CurrentUser.OpenSubKey(runKey, true))
            {
                if (enable)
                {
                    key.SetValue(appName, appPath);
                }
                else
                {
                    key.DeleteValue(appName, false);
                }
            }
        }


    }
}


